<template>
  <div>
    <BasicTable @register="departmentTable">
      <template #toolbar>
        <a-button type="primary" @click="handleCreate" v-auth="AuthCode.departmentAdd">
          新增部门
        </a-button>
        <Button type="primary" @click="download" v-auth="AuthCode.departmentExport">
          <DownloadOutlined />
          导出
        </Button>
        <Upload
          type="primary"
          :customRequest="upload"
          v-auth="AuthCode.departmentImport"
          :showUploadList="false"
        >
          <Button type="primary">
            <UploadOutlined />
            导入
          </Button>
        </Upload>
      </template>
      <template #action="{ record }">
        <div class="flex justify-around">
          <Button type="primary" @click="handleEdit(record)" v-auth="AuthCode.departmentUpdate">
            <Icon icon="clarity:note-edit-line" />
            编辑
          </Button>
          <div v-auth="AuthCode.departmentDestroy">
            <PopConfirmButton
              :title="`确定删除-${record.name}?`"
              @confirm="handleDelete(record)"
              ok-text="确定"
              danger
            >
              <Icon icon="ant-design:delete-outlined" /> 删除
            </PopConfirmButton>
          </div>
        </div>
      </template>
    </BasicTable>
    <DepartmentModel @register="registerModal" @success="handleSuccess" />
  </div>
</template>

<script lang="ts" setup>
  import { BasicTable, useTable } from '/@/components/Table';
  import { columns, searchFormSchema } from './data';
  import { deleteDepartment, exportDepartment, getDepartmentList } from '/@/api/sys/department';
  import { PopConfirmButton, Button } from '/@/components/Button';
  import { Upload } from 'ant-design-vue';
  import { useModal } from '/@/components/Modal';
  import Icon from '/@/components/Icon/index';
  import DepartmentModel from './DepartmentModal.vue';
  import { Recordable } from 'vite-plugin-mock';
  import { DeptListItem } from '/@/api/sys/model/departmentModel';
  import { AuthCode } from '/@/settings/authSetting';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { DownloadOutlined, UploadOutlined } from '@ant-design/icons-vue';
  import { downloadByData } from '/@/utils/file/download';

  const { createMessage } = useMessage();

  const [registerModal, { openModal }] = useModal();
  const [departmentTable, { reload, getRowSelection }] = useTable({
    title: '部门列表',
    api: getDepartmentList,
    rowKey: 'id',
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
    },
    pagination: false,
    striped: false,
    useSearchForm: true,
    rowSelection: {},
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    canResize: true,
    actionColumn: {
      title: '操作',
      dataIndex: 'action',
      slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  // 新增部门
  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  // 编辑部门
  function handleEdit(record: Recordable) {
    openModal(true, {
      isUpdate: true,
      record,
    });
  }

  // 删除部门
  async function handleDelete(record: DeptListItem) {
    try {
      await deleteDepartment(record);
      await reload();
    } catch (e) {
      console.log(e);
    }
  }

  // 导出部门
  const download = async () => {
    let records = await getRowSelection();
    let { selectedRowKeys: id_list } = records;
    if (id_list!.length < 1) {
      createMessage.warning('请勾选要导出的列');
      return;
    }
    let res = await exportDepartment(id_list as number[]);
    downloadByData(
      res.data,
      decodeURI(res.headers['content-disposition'].split(';')[1].split('=')[1]),
    );
  };

  const upload = async (res) => {
    console.log(res);
  };

  function handleSuccess() {
    reload();
  }
</script>

<style scoped lang="less"></style>
